//
//  OrderListCollectionViewController.m
//  FreightAssistant
//
//  Created by leo on 15/10/27.
//  Copyright © 2015年 leo. All rights reserved.
//

#import "OrderItemListViewController.h"

#import "OrderItemDetailViewController.h"

#import "LogisticsReslutViewController.h"

@interface OrderItemListViewController () <UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout> {
    
    //  采购单
    KKPurchaseOrderItemList *_purchaseOrderItemList;
    
    //  代购单
    KKSupplyOrderItemList *_supplyOrderItemList;
}

@end

@implementation OrderItemListViewController

- (void)loadView {
    [super loadView];
    
    UIView *rootView = [self view];
    
    CGSize itemSize = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT - 64 - 50);
    
    UICollectionViewFlowLayout *flowLayout = [UICollectionViewFlowLayout new];
    flowLayout.itemSize = CGSizeMake(itemSize.width, itemSize.height );
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.minimumInteritemSpacing = CGFLOAT_MIN;
    flowLayout.minimumLineSpacing = CGFLOAT_MIN;
    
    _kk_orderListCollectionView.backgroundColor = [UIColor colorWithWhite:0.950 alpha:1.000];
    [rootView addSubview: _kk_orderListCollectionView];
    
    _kk_orderListCollectionView.delegate = self;
    _kk_orderListCollectionView.dataSource = self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.edgesForExtendedLayout = UIRectEdgeNone;
    
    self.showMainItem = true;
    
    _supplyOrderItemList = [KKSupplyOrderItemList new];
    _purchaseOrderItemList = [KKPurchaseOrderItemList new];
    
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear: animated];
    
    if (_kk_segmentedControl.selectedSegmentIndex == 0) {
        
        [self downloadSupplyOrderListFromDataBase];
    }
    else {
        [self downloadPurchaseOrderListFromDataBase];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Action methods

- (IBAction)switchOrderType:(UISegmentedControl *)control {
    
    if (control.selectedSegmentIndex == 0) {
        
        [self downloadSupplyOrderListFromDataBase];
    }
    else {
        [self downloadPurchaseOrderListFromDataBase];
    }
}

#pragma mark - Private methods

- (void)updateUI {
    
    if (_kk_segmentedControl.selectedSegmentIndex == 0) {
    //  注册collectionViewCell
        [_kk_orderListCollectionView registerClass: [KKSupplyOrderCollectionViewCell class] forCellWithReuseIdentifier: NSStringFromClass([KKSupplyOrderCollectionViewCell class])];
    }
    else {
    //  注册collectionViewCell
        [_kk_orderListCollectionView registerClass: [KKPurchaseOrderCollectionViewCell class] forCellWithReuseIdentifier: NSStringFromClass([KKPurchaseOrderCollectionViewCell class])];
    }
    
    [_kk_orderListCollectionView reloadData];
}

- (void)eventOfCellWithItem:(KKOrderItem *)item type:(NSInteger)type {
    
    if ([item isKindOfClass: [KKSupplyOrderItem class]]) {
        
        switch (type) {
            case KKSupplyOrderCollectionEventTypeIsDetail:
            {
                OrderItemDetailViewController *detailViewController = [OrderItemDetailViewController viewControllerFromStoryboard];
                detailViewController.hidesBottomBarWhenPushed = true;
                detailViewController.kk_orderItem = item;
                [self.navigationController pushViewController: detailViewController animated: true];
            }
                break;
            case KKSupplyOrderCollectionEventTypeIsEdit:
            {
                
            }
                break;
            case KKSupplyOrderCollectionEventTypeIsLogisticsDetail:
            {
                LogisticsReslutViewController *resultViewController = [LogisticsReslutViewController new];
                [self.navigationController pushViewController: resultViewController animated: true];
            }
                break;
                
            default:
                break;
        }
    }
    else {
        switch (type) {
            case KKPurchaseOrderCollectionEventTypeIsDetail:
            {
                OrderItemDetailViewController *detailViewController = [OrderItemDetailViewController viewControllerFromStoryboard];
                detailViewController.hidesBottomBarWhenPushed = true;
                detailViewController.kk_orderItem = item;
                [self.navigationController pushViewController: detailViewController animated: true];
            }
                break;
            case KKPurchaseOrderCollectionEventTypeIsEdit:
            {
                
            }
                break;
            case KKPurchaseOrderCollectionEventTypeIsLogisticsDetail:
            {
                LogisticsReslutViewController *resultViewController = [LogisticsReslutViewController new];
                [self.navigationController pushViewController: resultViewController animated: true];
            }
                break;
                
            default:
                break;
        }
    }
}

#pragma mark - Request methods

- (void)downloadPurchaseOrderListFromDataBase {
    
    __weak typeof(self) weakSelf = self;
    __weak typeof(_purchaseOrderItemList) weakItemList = _purchaseOrderItemList;
    [KKDataBaseAPIInterface searchPurchaseOrderItemsWithEnabled: true successful:^(id object) {
        
//        NSLog(@"object = %@", object);
        [weakItemList appendItemsWithArray: object];
        [weakSelf updateUI];
        
    } faile:^(NSString *error) {
        
    }];
}

- (void)downloadSupplyOrderListFromDataBase {
    
    __weak typeof(self) weakSelf = self;
    __weak typeof(_supplyOrderItemList) weakItemList = _supplyOrderItemList;
    [KKDataBaseAPIInterface searchSupplyOrderItemsWithEnabled: true successful:^(id object) {
        
//        NSLog(@"object = %@", object);
        [weakItemList appendItemsWithArray: object];
        [weakSelf updateUI];
        
    } faile:^(NSString *error) {
        
    }];
}

#pragma mark - UICollectionView methods

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _kk_segmentedControl.selectedSegmentIndex == 0 ? _supplyOrderItemList.count : _purchaseOrderItemList.count;
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section {
    
    return UIEdgeInsetsMake(15, 0, 15, 0);
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return CGSizeMake(CGRectGetWidth(collectionView.bounds), 145);
}

- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    __weak typeof(self) weakSelf = self;
    
    if (_kk_segmentedControl.selectedSegmentIndex == 0) {
        KKSupplyOrderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: NSStringFromClass([KKSupplyOrderCollectionViewCell class]) forIndexPath: indexPath];
        
        KKSupplyOrderItem *item = [_supplyOrderItemList objectAtIndex: indexPath.row];
        [cell setKk_currentOrderItem: item];
        cell.eventBlock = ^(NSInteger eventType){
            [weakSelf eventOfCellWithItem: item type: eventType];
        };
        
        return cell;
    }
    else {
        KKPurchaseOrderCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier: NSStringFromClass([KKPurchaseOrderCollectionViewCell class]) forIndexPath: indexPath];
        
        KKPurchaseOrderItem *item = [_purchaseOrderItemList objectAtIndex: indexPath.row];
        [cell setKk_currentOrderItem: item];
        cell.eventBlock = ^(NSInteger eventType){
            [weakSelf eventOfCellWithItem: item type: eventType];
        };
        
        return cell;
    }
}

@end
